<?php


namespace app\cms\admin\controller\saas;


use app\cms\authCallBack\saas;
use app\cms\dto\saas\saasClientServerDbInfo;
use app\cms\model\saas\saasClientServerDbModel;
use iflow\auth\Authorize\AuthAnnotation;
use iflow\Response;
use iflow\Router\Controller;
use iflow\Router\implement\Request\{ GetMapping, PostMapping, DeleteMapping };

#[Controller('%cmsAdmin%/saas_db_server')]
class saasDbServerController {

    /**
     * 获取Saas数据库服务器
     * @param string $server_db_name
     * @param string $server_db_ip
     * @param string $server_db_type
     * @param int $status
     * @return Response
     */
    #[GetMapping]
    #[AuthAnnotation('saasAdmin', 'saasServerRead|saasServerReadWrite', saas::class . '@validIsSaasAccountValidServer')]
    public function getSaasDbServerList(
        string $server_db_name, string $server_db_ip, string $server_db_type, int $status = -1
    ): Response {
        return (new saasClientServerDbModel()) -> getSaasClientDbServer(
            $server_db_name, $server_db_ip, $server_db_type, $status
        );
    }

    /**
     * 创建saas节点服务数据库
     * @param saasClientServerDbInfo $saasClientServerDbInfo
     * @return Response
     */
    #[PostMapping]
    #[AuthAnnotation('saasAdmin', 'saasServerWrite|saasServerReadWrite', saas::class . '@validIsSaasAccountValidServer')]
    public function createDbServer(
        saasClientServerDbInfo $saasClientServerDbInfo
    ): Response {
        return (new saasClientServerDbModel())
            -> createData([
                'server_db_ip' => $saasClientServerDbInfo -> server_db_ip,
                'server_db_name' => $saasClientServerDbInfo -> server_db_name,
                'server_db_user' => $saasClientServerDbInfo -> server_db_user,
                'server_db_port' => $saasClientServerDbInfo -> server_db_port,
                'server_db_password' => $saasClientServerDbInfo -> server_db_password,
                'server_db_type' => $saasClientServerDbInfo -> server_db_type
            ]);
    }

    /**
     * 更新Saas节点服务数据库
     * @param saasClientServerDbInfo $saasClientServerDbInfo
     * @return Response
     */
    #[PostMapping]
    #[AuthAnnotation('saasAdmin', 'saasServerWrite|saasServerReadWrite', saas::class . '@validIsSaasAccountValidServer')]
    public function updateSaasClientDbServer(
        saasClientServerDbInfo $saasClientServerDbInfo
    ): Response {
        return (new saasClientServerDbModel()) -> updateSaasClientDbServer($saasClientServerDbInfo);
    }

    /**
     * 删除Saas节点服务数据库
     * @param int $server_db_id
     * @return Response
     */
    #[DeleteMapping('delete_saas_client_db_server/<[0-9]{1,}:server_db_id>')]
    #[AuthAnnotation('saasAdmin', 'saasServerWrite|saasServerReadWrite', saas::class . '@validIsSaasAccountValidServer')]
    public function deleteSaasClientDbServer(
        int $server_db_id
    ): Response {
        return (new saasClientServerDbModel()) -> deleteData([
           'server_db_id' => $server_db_id
        ]);
    }
}